# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openjpeg
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}2"
pkgver=2.1.0
pkgrel=7
pkgdesc="An open source JPEG 2000 codec (mingw-w64)"
arch=('any')
url="http://www.openjpeg.org"
license=("BSD")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-cmake")
options=('staticlibs' 'strip')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/uclouvain/openjpeg/archive/version.${pkgver%.*}.tar.gz
        0001-fix-install-for-dlls.all.patch
        0003-versioned-dlls.mingw.patch
        0005-sock-jpip.all.patch
        0006-openjpeg-2.1.0-stdcall-for-all-win.patch)

sha256sums=('4afc996cd5e0d16360d71c58216950bcb4ce29a3272360eb29cadb1c8bce4efc'
            '6d9d88474f815ddcd16b230c96aeb27171a640c5636e52182ae7cd1a1b5e23a3'
            '756f93b18ac80ea1cf90186d01800fcbe4dc40b36f164816f327f47b028902e1'
            'b7b22dca26d5fd98ca5bc6ce775bbbabebc0e10fcf07fd0afb54d580699b8312'
            'fcbc87fc5e51721e25ad9e50e84c336c9a5902ee8b2f2bb0800a3ea3fd72988e')

prepare() {
  cd "${srcdir}/${_realname}-version.${pkgver%.*}"
  patch -p1 -i ${srcdir}/0001-fix-install-for-dlls.all.patch
  patch -p1 -i ${srcdir}/0003-versioned-dlls.mingw.patch
  patch -p1 -i ${srcdir}/0005-sock-jpip.all.patch
  patch -p1 -i ${srcdir}/0006-openjpeg-2.1.0-stdcall-for-all-win.patch
}

build() {
  msg "Build static version"
  [[ -d ${srcdir}/build-static-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-static-${MINGW_CHOST}
  mkdir ${srcdir}/build-static-${MINGW_CHOST}
  cd ${srcdir}/build-static-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=OFF \
    -DBUILD_TESTING=OFF \
    -DCMAKE_SYSTEM_PREFIX_PATH=${MINGW_PREFIX} \
    -DBUILD_MJ2=ON \
    -DBUILD_JPWL=ON \
    -DBUILD_JPIP=OFF \
    -DBUILD_JP3D=ON \
    ../${_realname}-version.${pkgver%.*}
  make

  msg "Build shared version"
  [[ -d ${srcdir}/build-shared-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-shared-${MINGW_CHOST}
  mkdir ${srcdir}/build-shared-${MINGW_CHOST}
  cd ${srcdir}/build-shared-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_TESTING=ON \
    -DCMAKE_SYSTEM_PREFIX_PATH=${MINGW_PREFIX} \
    -DBUILD_MJ2=ON \
    -DBUILD_JPWL=ON \
    -DBUILD_JPIP=ON \
    -DBUILD_JP3D=ON \
    -DBUILD_DOC=OFF \
    -DBUILD_PKGCONFIG_FILES=ON \
    ../${_realname}-version.${pkgver%.*}
  make
}

package() {
  cd "${srcdir}/build-static-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install

  cd "${srcdir}/build-shared-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install
}
